﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<QLPKWeb.Models.FormBean.PhanQuyen.PermissionsAllUsersForm>" %>



<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript" src="<%= ResolveUrl("~/Content/Scripts/User/PermissionsAllUsers.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Content/Scripts/Common/DinhNghiaSoLuongSectionDongMo.js") %>"></script>
    
    <script type="text/javascript">
        selectMenu("system-SetPermissionsForAllUsers");
    </script>

    <%Html.BeginForm(); %>
    <input type="hidden" id="NewPermissionsAllUserString" name="NewPermissionsAllUserString" value="" />


    <%--<div class="module">--%>
   <div class="box round first fullpage">
            <h4> Gán Quyền cho toàn bộ Người dùng</h4>
	
		<!-- end box / title -->
        <%--<div class="module_top"></div>--%>
        
        <table style="width: 100%">
            <tr>
                <td>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                                <td align="left" id="NoteMessage1">
                                    <strong>
                                        <%if (Model.message == "Cập nhật thành công !")
                                        {
                                        %>
                                            <div style="color: Blue"><%=Model.message%></div>
                                        <%
                                        }
                                        else if (Model.message == "Cập nhật thất bại, xin thử lại!")
                                        {%>
                                            <div style="color: Red"><%=Model.message%></div>
                                        <%}%>
                                    </strong>
                                </td>
                                <td align="right">                            
                                    <input type="button" name="Update" id="Button1"class="btn btn-grey"  value="Cập nhật" onclick="onUpdate();" />&nbsp;
                                    <input type="reset" value="Hủy thay đổi" class="btn btn-grey" />
                                </td>
                            </tr>
                    </table> 
        
                    <%--Chia hệ thống phân quyền thành từng block một--%>
                    <%
                        for (int d = 0; d < Model.Blocks.Count; d++ )
                    {%>
                
                    <p style="margin:0px 0px 10px 40px"><b>Phân quyền : <%=Model.Blocks[d].ToUpper()%> &nbsp;</b></p>
                    <div id="arrow<%=d + 1 %>" class="background_arrow_up arrow" style="float: left; cursor: pointer;
                        clear: both; margin-top: -32px; margin-right:0px">
                    </div>
                    <div id="open_close<%=d + 1 %>" class="bo" style="width: 100%; border: 1px; margin-left: 0%; margin-bottom: 15px;">
                        <table border="1" style="border-collapse:collapse;width: 100%" cellspacing="0" cellpadding="0">
                            <tr class="title">
                                <td style="border: 1px solid #ddd "class="first">
                                    &nbsp;<b>Chức năng</b>
                                </td>
                                <td style="border: 1px solid #ddd "width="21%" align="center" nowrap="nowrap">
                                    &nbsp;<b>Create/Edit/Update</b>
                                </td>
                                <td style="border: 1px solid #ddd "width="13%" align="center" nowrap="nowrap">
                                    &nbsp;<b>View/List</b>
                                </td>
                                <td style="border: 1px solid #ddd "width="11%" align="center" nowrap="nowrap">
                                    &nbsp;<b>Delete</b>
                                </td>
                                <td style="border: 1px solid #ddd "width="11%" align="center" class="last" nowrap="nowrap">
                                    &nbsp;<b>Execute</b>
                                </td>
                            </tr>     
            
             
                            <%for (int i = 0; i < Model.ModulesByBlockArray[d].Count; i++)
                            {%>
            

                            <tr onclick="doClickCheckbox();">
                                <td style="border: 1px solid #ddd ">
                                    &nbsp;<%=Model.ModulesByBlockArray[d][i]%>
                                </td>

                                <%--Case1 : Permission : Create/Edit/Update | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag1 = false;
                                    for (int j = 0; j < Model.PathsList.Count; j++)
                                    {
                                        if ((Model.PathsList[j].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j].PermissionType == "Create_Edit_Update"))
                                        {
                                            checkBoxFlag1 = true;
                                            bool checkedFlag = false;
                                            for (int k = 0; k < Model.PathsForAllUsersList.Count; k++)
                                            {
                                                if ((Model.PathsForAllUsersList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForAllUsersList[k].PermissionType == "Create_Edit_Update"))
                                                {%>
                                <td style="border: 1px solid #ddd "align="center">
                                    <input id="<%=
                                                        Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForAllUsersList[k].PermissionType%>"
                                        type="checkbox" value="<%=Model.PathsForAllUsersList[k].ID %>" checked="checked"
                                        onclick="unSelectCreate('<%=Model.ModulesByBlockArray[d][i]%>','<%=Model.PathsForAllUsersList[k].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                                }
                                            }

                                            if (checkedFlag == false)
                                            {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i]+ "-Create_Edit_Update"%>" type="checkbox" value="<%=Model.PathsList[j].ID %>"
                                        onclick="unSelectCreate('<%=Model.ModulesByBlockArray[d][i]%>','-Create_Edit_Update');" />&nbsp;
                                </td>
                                <%}
                                                break;
                                        }
                                    }
                                    if (checkBoxFlag1 == false)
                                    {
                                %>
                                <td align="center"style="border: 1px solid #ddd ">
                                    &nbsp;
                                </td>
                                <%
                                    }
                                
                                %>
                                <%--Case2 : Permission : View/List | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag2 = false;
                                    for (int j1 = 0; j1 < Model.PathsList.Count; j1++)
                                    {
                                        if ((Model.PathsList[j1].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j1].PermissionType == "View_List"))
                                        {
                                            checkBoxFlag2 = true;
                                            bool checkedFlag = false;
                                            for (int k = 0; k < Model.PathsForAllUsersList.Count; k++)
                                            {
                                                if ((Model.PathsForAllUsersList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForAllUsersList[k].PermissionType == "View_List"))
                                                {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForAllUsersList[k].PermissionType%>"
                                        value="<%=Model.PathsForAllUsersList[k].ID %>" type="checkbox" checked="checked"
                                        onclick="unSelectView('<%=Model.ModulesByBlockArray[d][i] %>','<%=Model.PathsForAllUsersList[k].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                                }
                                            }

                                            if (checkedFlag == false)
                                            {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i]+ "-View_List"%>" value="<%=Model.PathsList[j1].ID %>"
                                        type="checkbox" onclick="unSelectView('<%=Model.ModulesByBlockArray[d][i] %>','-View_List');" />&nbsp;
                                </td>
                                <%}
                                                break;
                                        }
                                    }
                                    if (checkBoxFlag2 == false)
                                    {
                                %>
                                <td align="center"style="border: 1px solid #ddd ">
                                    &nbsp;
                                </td>
                                <%
                                    }    
                                %>
                                <%--Case3 : Permission : Delete | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag3 = false;
                                    for (int j2 = 0; j2 < Model.PathsList.Count; j2++)
                                    {
                                        if ((Model.PathsList[j2].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j2].PermissionType == "Delete"))
                                        {
                                            checkBoxFlag3 = true;
                                            bool checkedFlag = false;
                                            for (int k = 0; k < Model.PathsForAllUsersList.Count; k++)
                                            {
                                                if ((Model.PathsForAllUsersList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForAllUsersList[k].PermissionType == "Delete"))
                                                {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForAllUsersList[k].PermissionType %>"
                                        type="checkbox" value="<%=Model.PathsForAllUsersList[k].ID %>" checked="checked"
                                        onclick="unSelectDelete('<%=Model.ModulesByBlockArray[d][i] %>','<%=Model.PathsForAllUsersList[k].PermissionType %>');" />&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                                }
                                            }

                                            if (checkedFlag == false)
                                            {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-Delete"%>" type="checkbox" value="<%=Model.PathsList[j2].ID %>"
                                        onclick="unSelectDelete('<%=Model.ModulesByBlockArray[d][i] %>','-Delete');" />&nbsp;
                                </td>
                                <%}
                                                break;
                                        }
                                    }
                                    if (checkBoxFlag3 == false)
                                    {
                                %>
                                <td align="center"style="border: 1px solid #ddd ">
                                    &nbsp;
                                </td>
                                <%
                                    }
                                %>
                                
                                <%--Case4 : Permission : Execute - Ngoài Thực thi một thao tác nào đó ngoài( Create/Edit/Update ; view/List ; Delete) | Build CheckBox--%>
                                <%
                                    bool checkBoxFlag4 = false;
                                    for (int j3 = 0; j3 < Model.PathsList.Count; j3++)
                                    {
                                        if ((Model.PathsList[j3].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsList[j3].PermissionType == "Execute"))
                                        {
                                            checkBoxFlag4 = true;
                                            bool checkedFlag = false;
                                            for (int k = 0; k < Model.PathsForAllUsersList.Count; k++)
                                            {
                                                if ((Model.PathsForAllUsersList[k].Modules == Model.ModulesByBlockArray[d][i]) && (Model.PathsForAllUsersList[k].PermissionType == "Execute"))
                                                {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-" + Model.PathsForAllUsersList[k].PermissionType %>"
                                        type="checkbox" value="<%=Model.PathsForAllUsersList[k].ID %>" checked="checked"/>&nbsp;
                                </td>
                                <%
                                    checkedFlag = true;
                                    break;
                                                }
                                            }

                                            if (checkedFlag == false)
                                            {%>
                                <td align="center"style="border: 1px solid #ddd ">
                                    <input id="<%=Model.ModulesByBlockArray[d][i] + "-Execute"%>" type="checkbox" value="<%=Model.PathsList[j3].ID %>"/>&nbsp;
                                </td>
                                <%}
                                                break;
                                        }
                                    }
                                    if (checkBoxFlag4 == false)
                                    {
                                %>
                                <td align="center"style="border: 1px solid #ddd ">
                                    &nbsp;
                                </td>
                                <%
                                    }
                                %>
                            </tr>
                            <%
                                }%>
                        </table>
        
                    </div>
                    <br/>
                
                    <%
                    }//end Vòng For cho Block
                    %>
                    <table border="0" cellspacing="0" cellpadding="0" class="table_module">
                        <tr>
                            <td align="left" id="NoteMessage2">
                                <strong>
                                    <%
                                        if (Model.message == "Cập nhật thành công !")
                                    {
                                    %>
                                    <div style="color: Blue"><%=Model.message%></div>
                                    <%
                                    }
                                    else if (Model.message == "Cập nhật thất bại, xin thử lại !")
                                    {%>
                                    <div style="color: Red"><%=Model.message%></div>
                                    <%}%>
                                </strong>
                            </td>
                            <td align="right">
                                <input type="button" name="Update" id="Button2" value="Cập nhật" onclick="onUpdate();" class="btn btn-grey"/>&nbsp;
                                <input type="reset" value="Hủy thay đổi" class="btn btn-grey"/>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <%Html.EndForm(); %>

    <script language="javascript" type="text/javascript">
    
        function onUpdate() {
            for (var i = 0; i < document.forms[0].length; i++) {
                var e = document.forms[0].elements[i];
                if (e.type == 'checkbox' && e.checked) {
                    document.getElementById("NewPermissionsAllUserString").value = document.getElementById("NewPermissionsAllUserString").value + "|" + e.id;
                }
            }
            document.forms[0].action = '<%=ResolveUrl("~/PermissionsAllUsers/SetPermissionsForAllUsersUpdate/")%>';
            document.forms[0].method = "Post";
            document.forms[0].submit();
        }

        function doClickCheckbox() {
            document.getElementById('NoteMessage1').innerHTML = "";
            document.getElementById('NoteMessage1').innerHTML = "";
        }
    </script>

</asp:Content>
